package io.renren.modules.gengyun.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.Min;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Date_Api {
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date date;

    public static HashMap<String, String> initData(String date) {
        String dates[] = date.split("/");
        int year = Integer.parseInt(dates[0]);
        int month = Integer.parseInt(dates[1]);
        int day = Integer.parseInt(dates[2]);
        boolean isConcatDate = true;
        int beginMonth = 13;
        int beginDay = 32;
        int endMonth = 13;
        int endDay = 32;
//        int month = date.;
        switch (month) {
            case 4:
                if (day <= 30 && day >= 15) {
                    beginMonth = 4;
                    beginDay = 15;
                    endMonth = 5;
                    endDay = 5;
                }
                break;
            case 5:
                if (day >= 1 && day <= 5) {
                    beginMonth = 4;
                    beginDay = 15;
                    endMonth = 5;
                    endDay = 5;
                } else if (day >= 15 && day <= 26) {
                    beginMonth = 5;
                    beginDay = 15;
                    endMonth = 5;
                    endDay = 26;
                } else if (day >= 27 && day <= 31) {
                    beginMonth = 5;
                    beginDay = 27;
                    endMonth = 6;
                    endDay = 2;
                }
                break;
            case 6:
                if (day >= 1 && day <= 2) {
                    beginMonth = 5;
                    beginDay = 27;
                    endMonth = 6;
                    endDay = 2;
                } else if (day >= 3 && day <= 9) {
                    beginMonth = 6;
                    beginDay = 3;
                    endMonth = 6;
                    endDay = 9;
                } else if (day >= 10 && day <= 16) {
                    beginMonth = 6;
                    beginDay = 10;
                    endMonth = 6;
                    endDay = 16;
                } else if (day >= 17 && day <= 23) {
                    beginMonth = 6;
                    beginDay = 17;
                    endMonth = 6;
                    endDay = 23;
                } else if (day >= 23 && day <= 30) {
                    beginMonth = 6;
                    beginDay = 24;
                    endMonth = 6;
                    endDay = 30;
                }
                break;
            case 7:
                if (day >= 1 && day <= 7) {
                    beginMonth = 7;
                    beginDay = 1;
                    endMonth = 7;
                    endDay = 7;
                } else if (day >= 8 && day <= 14) {
                    beginMonth = 7;
                    beginDay = 8;
                    endMonth = 7;
                    endDay = 14;
                } else if (day >= 15 && day <= 21) {
                    beginMonth = 7;
                    beginDay = 15;
                    endMonth = 7;
                    endDay = 21;
                } else if (day >= 22 && day <= 28) {
                    beginMonth = 7;
                    beginDay = 22;
                    endMonth = 7;
                    endDay = 28;
                } else if (day >= 29 && day <= 31) {
                    beginMonth = 7;
                    beginDay = 29;
                    endMonth = 8;
                    endDay = 4;
                }
                break;
            case 8:
                if (day >= 1 && day <= 4) {
                    beginMonth = 8;
                    beginDay = 5;
                    endMonth = 8;
                    endDay = 11;
                } else if (day >= 5 && day <= 11) {
                    beginMonth = 8;
                    beginDay = 12;
                    endMonth = 8;
                    endDay = 18;
                } else if (day >= 12 && day <= 18) {
                    beginMonth = 8;
                    beginDay = 12;
                    endMonth = 8;
                    endDay = 18;
                } else if (day >= 19 && day <= 25) {
                    beginMonth = 8;
                    beginDay = 19;
                    endMonth = 9;
                    endDay = 25;
                } else if (day >= 26 && day <= 31) {
                    beginMonth = 8;
                    beginDay = 26;
                    endMonth = 9;
                    endDay = 1;
                }
                break;
            case 9:
                if (day == 1) {
                    beginMonth = 8;
                    beginDay = 26;
                    endMonth = 9;
                    endDay = 1;
                } else if (day >= 2 && day <= 8) {
                    beginMonth = 9;
                    beginDay = 2;
                    endMonth = 9;
                    endDay = 8;
                } else if (day >= 9 && day <= 15) {
                    beginMonth = 9;
                    beginDay = 9;
                    endMonth = 9;
                    endDay = 15;
                } else if (day >= 16 && day <= 22) {
                    beginMonth = 9;
                    beginDay = 16;
                    endMonth = 9;
                    endDay = 22;
                } else if (day >= 23 && day <= 30) {
                    beginMonth = 9;
                    beginDay = 23;
                    endMonth = 9;
                    endDay = 30;
                }
                break;
            default:
                isConcatDate = false;
                break;
        }
        if (isConcatDate) {
            return concatDate(year, beginMonth, beginDay, endMonth, endDay);
        }
        return null;
    }

    private static HashMap<String, String> concatDate(int year, int beginMonth, int beginDay, int endMonth, int endDay) {
        String beginTimeText = year + "-" + beginMonth + "-" + beginDay + " " + "00:00:00";
        String endTimeText = year + "-" + endMonth + "-" + endDay + " " + "23:59:59";
        return getStringStringHashMap(beginTimeText, endTimeText);
    }

    public static HashMap<String, String> initHourlyData(String date, String time) {
        String dates[] = date.split("/");
        int year = Integer.parseInt(dates[0]);
        int month = Integer.parseInt(dates[1]);
        int day = Integer.parseInt(dates[2]);
        int hourly = Integer.parseInt(time);
        int startHour = hourly;
        int endHour = hourly+12;
        if (endHour > 24) {
            endHour -=12;
            startHour -=12;
        }
        String beginTimeText = year + "-" + month + "-" + day + " " + startHour + ":00:00";
        String endTimeText = year + "-" + month + "-" + day + " " + endHour + ":00:00";
        return getStringStringHashMap(beginTimeText, endTimeText);
    }

    private static HashMap<String, String> getStringStringHashMap(String beginTimeText, String endTimeText) {
        long beginTime = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(beginTimeText, new ParsePosition(0)).getTime();
        long endTime = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(endTimeText, new ParsePosition(0)).getTime();
        HashMap<String, String> period = new HashMap<>();
        period.put("beginTime", String.valueOf(beginTime));
        period.put("endTime", String.valueOf(endTime));
        return period;
    }

    public static HashMap<String, String> getCompareDates(String beginYear) throws ParseException {
//        dates[0]
//        ArrayList<String> years = new ArrayList<>();
//        for(String string : dates){
//            SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
//            simpleDateFormat.parse(string);
//            years.add(string);
//        }

        String[] date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()).split("-");
        String endYear = date[0];
//        System.out.println(endTimeTemp);

        String beginTimeText = String.valueOf(Math.min(Integer.parseInt(beginYear),Integer.parseInt(endYear)));
        String endTimeText =String.valueOf(Math.max(Integer.parseInt(beginYear),Integer.parseInt(endYear)));
        beginTimeText += "-01-01 00:00:00";
        endTimeText += "-12-31 23:59:59";
        return getStringStringHashMap(beginTimeText,endTimeText);
    }


    public static String initPlanData(String time) {
        String dates[] = time.split("/");
        int month = Integer.parseInt(dates[1]);
        int day = Integer.parseInt(dates[2]);
        String returnMonth = String.valueOf(month);
        String returnDay = String.valueOf(day);
        if (month<10){
            returnMonth = "0"+month;
        }
        if (day<10){
            returnDay = "0"+month;
        }
        String date = returnMonth+"-"+returnDay;
        System.out.println(date);
//        int month = date.;
        return date;

    }



//    public static HashMap<String, String> initPlanData(String time) {
//        String dates[] = time.split("/");
//        int year = Integer.parseInt(dates[0]);
//        int month = Integer.parseInt(dates[1]);
//        int day = Integer.parseInt(dates[2]);
//        boolean isConcatDate = true;
//        int beginMonth = 13;
//        int beginDay = 32;
//        int endMonth = 13;
//        int endDay = 32;
////        int month = date.;
//        switch (month) {
//            case 4:
//                if (day <= 30 && day >= 15) {
//                    beginMonth = 4;
//                    beginDay = 15;
//                    endMonth = 5;
//                    endDay = 5;
//                }
//                break;
//            case 5:
//                if (day >= 1 && day <= 5) {
//                    beginMonth = 4;
//                    beginDay = 15;
//                    endMonth = 5;
//                    endDay = 5;
//                } else if (day >= 15 && day <= 26) {
//                    beginMonth = 5;
//                    beginDay = 15;
//                    endMonth = 5;
//                    endDay = 26;
//                } else if (day >= 27 && day <= 31) {
//                    beginMonth = 5;
//                    beginDay = 27;
//                    endMonth = 6;
//                    endDay = 2;
//                }
//                break;
//            case 6:
//                if (day >= 1 && day <= 2) {
//                    beginMonth = 5;
//                    beginDay = 27;
//                    endMonth = 6;
//                    endDay = 2;
//                } else if (day >= 3 && day <= 9) {
//                    beginMonth = 6;
//                    beginDay = 3;
//                    endMonth = 6;
//                    endDay = 9;
//                } else if (day >= 10 && day <= 16) {
//                    beginMonth = 6;
//                    beginDay = 10;
//                    endMonth = 6;
//                    endDay = 16;
//                } else if (day >= 17 && day <= 23) {
//                    beginMonth = 6;
//                    beginDay = 17;
//                    endMonth = 6;
//                    endDay = 23;
//                } else if (day >= 23 && day <= 30) {
//                    beginMonth = 6;
//                    beginDay = 24;
//                    endMonth = 6;
//                    endDay = 30;
//                }
//                break;
//            case 7:
//                if (day >= 1 && day <= 7) {
//                    beginMonth = 7;
//                    beginDay = 1;
//                    endMonth = 7;
//                    endDay = 7;
//                } else if (day >= 8 && day <= 14) {
//                    beginMonth = 7;
//                    beginDay = 8;
//                    endMonth = 7;
//                    endDay = 14;
//                } else if (day >= 15 && day <= 21) {
//                    beginMonth = 7;
//                    beginDay = 15;
//                    endMonth = 7;
//                    endDay = 21;
//                } else if (day >= 22 && day <= 28) {
//                    beginMonth = 7;
//                    beginDay = 22;
//                    endMonth = 7;
//                    endDay = 28;
//                } else if (day >= 29 && day <= 31) {
//                    beginMonth = 7;
//                    beginDay = 29;
//                    endMonth = 8;
//                    endDay = 4;
//                }
//                break;
//            case 8:
//                if (day >= 1 && day <= 4) {
//                    beginMonth = 8;
//                    beginDay = 5;
//                    endMonth = 8;
//                    endDay = 11;
//                } else if (day >= 5 && day <= 11) {
//                    beginMonth = 8;
//                    beginDay = 12;
//                    endMonth = 8;
//                    endDay = 18;
//                } else if (day >= 12 && day <= 18) {
//                    beginMonth = 8;
//                    beginDay = 12;
//                    endMonth = 8;
//                    endDay = 18;
//                } else if (day >= 19 && day <= 25) {
//                    beginMonth = 8;
//                    beginDay = 19;
//                    endMonth = 9;
//                    endDay = 25;
//                } else if (day >= 26 && day <= 31) {
//                    beginMonth = 8;
//                    beginDay = 26;
//                    endMonth = 9;
//                    endDay = 1;
//                }
//                break;
//            case 9:
//                if (day == 1) {
//                    beginMonth = 8;
//                    beginDay = 26;
//                    endMonth = 9;
//                    endDay = 1;
//                } else if (day >= 2 && day <= 8) {
//                    beginMonth = 9;
//                    beginDay = 2;
//                    endMonth = 9;
//                    endDay = 8;
//                } else if (day >= 9 && day <= 15) {
//                    beginMonth = 9;
//                    beginDay = 9;
//                    endMonth = 9;
//                    endDay = 15;
//                } else if (day >= 16 && day <= 22) {
//                    beginMonth = 9;
//                    beginDay = 16;
//                    endMonth = 9;
//                    endDay = 22;
//                } else if (day >= 23 && day <= 30) {
//                    beginMonth = 9;
//                    beginDay = 23;
//                    endMonth = 9;
//                    endDay = 30;
//                }
//                break;
//            default:
//                isConcatDate = false;
//                break;
//        }
//        if (isConcatDate) {
//            return concatDateNoTime(year, beginMonth, beginDay, endMonth, endDay);
//        }
//        return null;
//
//    }

    private static HashMap<String, String> concatDateNoTime(int year, int beginMonth, int beginDay, int endMonth, int endDay) {
        String beginTimeText = year + "-" + beginMonth + "-" + beginDay;
        String endTimeText = year + "-" + endMonth + "-" + endDay;
        System.out.println(beginTimeText+" "+endTimeText);
        HashMap<String, String> period = new HashMap<>();
        period.put("beginTime", beginTimeText);
        period.put("endTime", endTimeText);
        return period;
    }
}
